;
(function (call) {
    var main = {
        init: function () {
            this.initView();
            this.bindEvt();
        },
        initView: function () {
            var userInfo = $("#userInfo").data('info');
            this.userInfo = userInfo;
            $("#dutyPerson").val(userInfo.smuser.name);
            this.initTime();
            this.getXybDataDetail();
        },
        // 格式化时间
        initTime: function () {
            var time = new Date();
            $("#publishTime").val(time.format('yyyy.MM.dd'));
            var date = Number(time.format('dd'));
            var month = time.format('MM月');
            var year = time.format('yyyy年');
            var month2 = new Date();
            var lastDay = time.setMonth(time.getMonth() + 1);
            var lastDay = time.setDate(0);
            lastDay = new Date(lastDay).format('dd');
            var xybDate = '', nextTime = '';
            if (date <= 10) {
                xybDate = '中旬'
                nextTime = '19'
            } else if (date > 11 && date <= 20) {
                xybDate = '下旬'
                nextTime = Number(lastDay) - 1
            } else {
                xybDate = '上旬'
                nextTime = '09'
				month2.setDate(1);
                month2.setMonth(month2.getMonth() + 1);
                month = month2.format('MM月');
                if (month == '01月') {
                    year = new Date().getFullYear() + 1
                    year = year + '年';
                }
            }
            $("#xybDate1").val(year + month + xybDate);
            $("#nextPublishTime").val(month + nextTime + '日');
        },
        // 初始化旬预报
        initXybData: function () {
            var that = this;
            var params = {
                queryTime: new Date().format('yyyyMMdd200000')
            }
            loading.add('.cafe-container');
            $.ajax({
                type: "get",
                url: api.external.getXybStatCompMsg,
                data: params
            }).done(function (data) {
                loading.remove();
                debugger
                $("#weatherCount").val(data);
                
            });
            $("#weatherForecast,#processPre").val('');
            $.ajax({
                type: "get",
                url: api.external.getXybStatTabData,
                dataType: 'json',
                data: params
            }).done(function (data2) {
                loading.remove();
                that.buildXybTable(data2);
            });
        },
        // 展示旬预报表格
        buildXybTable: function (data2) {
            var $table = $("#xybTable").empty();
            for (var i = 0; i < data2.length; i++) {
                var area = data2[i][0]
                var tr = '<tr><td><label class="tpVal area">' + area + '</label><div><span>旬平均气温(℃)</span>'
                tr += '<span>旬最低气温(℃)</span><span>旬最高气温(℃)</span> <span>旬降水量(mm)</span></div></td><td>'
                for (var j = 0; j < data2[i].length; j++) {
                    var item = data2[i][j]
                    if (j != 0) {
                        tr += '<input type="text" class="tpVal" value="' + item + '">'
                    }
                }
                tr += '</td></tr>'
                $table.append(tr);
            }
        },
        // 发布旬预报,先请求图片,后发布
        publishXybData: function () {
            var that = this, xybDate = [];
            var productInfo = $(".task-menu-list li.active").data('product');
            var queryTime = new Date().format('yyyyMMdd200000');
            $("#xybTable tr").each(function (i, item) {
                var obj = $(this), list = [];
                obj.find(".tpVal").each(function () {
                    if ($(this).hasClass("area")) {
                        list.push($(this).text());
                    } else {
                        list.push($(this).val());
                    }
                })
                xybDate.push(list)
            })
            xybDate = JSON.stringify(xybDate);
            var params2 = {
                queryTime: queryTime,
                tabData: xybDate
            }
            loading.add('.cafe-container');
            $.ajax({
                type: "get",
                url: api.external.getXybBackGround,
                data: params2
            }).done(function (res) {
                loading.remove();
                var data = res;
                if (!data) {
                    data = ''
                }
                var jsonStr = {
                    "tempData": {
                        "curTagData": {
                            "i_xyb01_chair10": { "val": $("#dutyPerson1").val() },
                            "i_xyb01_date01": { "val": $("#publishTime").val() },
                            "i_xyb01_date02": { "val": $("#xybDate1").val() },
                            "i_xyb01_date08": { "val": $("#nextPublishTime").val() },
                            "i_xyb01_fore09": { "val": $("#dutyPerson").val() },
                            "i_xyb01_msg03": { "val": $("#weatherForecast").val() },
                            "i_xyb01_msg04": { "val": $("#weatherCount").val() },
                            "i_xyb01_msg05": { "val": $("#processPre").val() },
                            "k_xyb01_msg06": { "val": xybDate },
                            "p_xyb01_pic07": { "val": data }
                        },
                    }
                }
                var params = {
                    product_id: productInfo.productId,
                    fortime: productInfo.fortime,
                    forecasterId: that.userInfo.smuser.iid,
                    jsonStr: JSON.stringify(jsonStr),
					taskId:productInfo.taskId
                }
                $.post(api.internal.xyb.pushforeXYB, params, function (res2) {
                    if (res2.isSuccess) {
                        alert("发布成功");
                        that.getXybDataDetail();
                        CafeJS.menuFunc.setTask();
                    } else {
                        alert("发布失败");
                    }
                })
            });
        },
        // 获取旬预报数据
        getXybDataDetail: function () {
            var that = this;
            var productInfo = $(".task-menu-list li.active").data('product');
            var time = new Date().format('yyyy-MM-dd 20:00:00')
            var params = {
                product_id: productInfo.productId,
                fortime: productInfo.fortime,
                queryTime: time
            }
            $.getJSON(api.internal.common.getProductDetail, params, function (res) {
                if (!res.isSuccess) return;
                if (JSON.stringify(res.data) == "{}") {
                    that.initXybData();
                } else {
                    that.getXybData(res.data);
                }
            })
        },
        // 获取旬预报数据
        getXybData: function (data) {
            var item = JSON.parse(data.data.content)
            item = item.tempData.curTagData
            $("#dutyPerson1").val(item.i_xyb01_chair10.val)
            $("#publishTime").val(item.i_xyb01_date01.val)
            $("#xybDate1").val(item.i_xyb01_date02.val)
            $("#nextPublishTime").val(item.i_xyb01_date08.val)
            $("#dutyPerson").val(item.i_xyb01_fore09.val)
            $("#weatherForecast").val(item.i_xyb01_msg03.val)
            $("#weatherCount").val(item.i_xyb01_msg04.val)
            $("#processPre").val(item.i_xyb01_msg05.val)
            this.buildXybTable(JSON.parse(item.k_xyb01_msg06.val))
            this.productPreview(data.data.fileName);
        },
        // 产品预览
        productPreview: function (filePath) {
            $("#contentFrame").attr("src", "http://10.226.120.202:80//ioffice/officeView.do?path=" + encodeURI(encodeURI(filePath)));
        },
        // 校验发布时间是否为每旬的倒数第二天
        inspectionTime: function () {
            var days = [30, 27, 30, 29, 30, 29, 30, 30, 29, 30, 29, 30];
            var date = new Date();
            var year = date.getFullYear(),
                today = date.getDate(),
                month = date.getMonth();
            if (month == 1) {// 如果2月有29天，则下旬的倒数第二天为28号。
                maxDate = new Date(year, month + 1, 0).getDate();
                if (maxDate == 29)
                    days[1] = 28;
            }
            if (today != 9 && today != 19 && today != days[month]) {
                //alert("旬预报只能在每旬的倒数第二天才可以发布");
                //return;
            }
            this.publishXybData();
        },
        bindEvt: function () {
            var that = this;
            $("#publishBtn").click(function () {
                that.inspectionTime();
                // this.publishXybData();
            })
            $("#returnBtn").click(function () {
                that.initXybData();
                that.initTime();
            })
        },
    };
    if (typeof call === 'function') {
        call(main);
    }
})(function (main) {
    main.init();
})